
import router from '@/router'
import store from '@/store'
import defautSetting from '@/settings'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

const whiteList = ['/login', '/404']

router.beforeEach((to, from, next) => {
  NProgress.start() // 开启进度
  const token = store.getters.token
  if (token) {
    if (to.path === '/login') {
      next('/dashboard')
    } else {
      // store.dispatch('user/getInfo')
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  NProgress.done()
})

router.afterEach((to) => {
  NProgress.done() // 结束进度
  if (to.meta.title) { document.title = defautSetting.title + '-' + to.meta.title } else { document.title = defautSetting.title }
})
